import com.you.rpc.RpcClient;
import com.you.rpc.RpcServer;
import com.you.rpc.server.UserProcessor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;

@Slf4j
public class SimpleTest2 {
    @Data
    @AllArgsConstructor
    public static class Request implements Serializable {
        int id;
        String name;
    }

    @Data
    @AllArgsConstructor
    public static class Response implements Serializable {
        float avg;
        String info;
    }

    @Test
    void sever() throws InterruptedException {
        RpcServer rpcServer = new RpcServer("localhost", 8888);
        rpcServer.registerUserProcessor(new UserProcessor<Request>() {
            @Override
            public Object handlerRequest(Request request) {
                log.warn("{}", request);
                String info = "HelloWorld!" + request;
                Response response = new Response((float) request.getId() * 100 / 7, info);
                log.warn("{}", response);
                return response;
            }
        });
        rpcServer.start();
        Thread.sleep(10000000);
    }

    @Test
    void client() throws Exception {
        RpcClient rpcClient = new RpcClient();
        rpcClient.start();
        String serverAddress = "localhost:8888";
        String name = "HiWorld!";
        int id = ThreadLocalRandom.current().nextInt();
        Request request = new Request(id, name);
        Thread.sleep(1000);
        log.warn("{}", request);
        Response response = (Response) rpcClient.invokeSync(serverAddress, request);
        log.warn("{}", response);
        Thread.sleep(10000000);
    }

}
